package com.aimuceliang.aimuceliang.ui.activity;

import android.widget.EditText;

import com.aimuceliang.aimuceliang.MyApp;
import com.aimuceliang.aimuceliang.bean.HttpResult;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.net.HttpSubscriber;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.utils.StringUtil;
import com.aimuceliang.aimuceliang.utils.T;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;

/**
 * Created by ziabo on 2017/7/27.
 * 申请账号
 */

public class ApplyAccountActivity extends BaseActivity {
    @BindView(R.id.et_company)
    EditText vEtCompany;
    @BindView(R.id.et_name)
    EditText vEtName;
    @BindView(R.id.et_email)
    EditText vEtEmail;
    @BindView(R.id.et_phone)
    EditText vEtPhone;

    @Override
    protected int initContentView() {
        return R.layout.activity_apply_account;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("账号申请");
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.bt_apply)
    public void onClicks() {
        String company = vEtCompany.getText().toString().trim();
        String name = vEtName.getText().toString().trim();
        String email = vEtEmail.getText().toString().trim();
        String phone = vEtPhone.getText().toString().trim();
        if (!StringUtil.checkStr(company)) {
            T.showShort("公司名不能为空");
            return;
        }
        if (!StringUtil.checkStr(name)) {
            T.showShort("姓名不能为空");
            return;
        }
        if (!StringUtil.checkStr(email)) {
            T.showShort("邮箱不能为空");
            return;
        }
        if (!StringUtil.checkStr(phone)) {
            T.showShort("手机号不能为空");
            return;
        } else {
            if (!StringUtil.isPhone(phone)) {
                T.showShort("请输入正确格式的手机号");
                return;
            }
        }
        Subscription s = MyApp.getApiService().register(new HttpSubscriber<HttpResult>() {
            @Override
            public void onFinished() {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                if (httpResult != null) {
                    if (StringUtil.checkStr(httpResult.code)) {
                        if (httpResult.code.equals("0")) {
                            startActivity(LoginActivity.class);
                            finish();
                        }
                        T.showShort(httpResult.message);
                    }
                }
            }
        }, company, name, email, phone);
        addSubscription(s);
    }
}
